from django.db import models
from django.conf import settings
from django.utils.timezone import now

# Create your models here.

class UserDevice(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='devices'
    )
    device_id = models.CharField(max_length=64, unique=True)
    device_name = models.CharField(max_length=255)
    device_type = models.CharField(max_length=20)  # mobile, desktop, tablet etc.
    browser = models.CharField(max_length=50)
    os = models.CharField(max_length=50)
    ip_address = models.GenericIPAddressField()
    last_login = models.DateTimeField(default=now)
    is_active = models.BooleanField(default=True)
    token_version = models.PositiveIntegerField(default=1)  
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ['-last_login']
        verbose_name = 'User Device'
        verbose_name_plural = 'User Devices'
        
    def __str__(self):
        return f"{self.device_name} ({self.user.username})"
